package com.me.customerdemo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.me.customerdemo.dao.CustomerMapper;
import com.me.customerdemo.pojo.Customer;
import com.me.customerdemo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CustomerServerImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public List<Customer> selectAll() {
        return customerMapper.selectAll();
    }

    @Override
    public boolean updateById(Customer customer) {
        return customerMapper.updateById(customer)>0? true:false;
    }

    @Override
    public boolean deleteById(int id) {
        return customerMapper.deleteById(id)>0?true:false;
    }

    @Override
    public boolean apply(Customer customer) {
        return customerMapper.apply(customer)>0?true:false;
    }

    @Override
    public PageInfo<Customer> findByKeyword(int pageNum, int pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        List<Customer> customers = customerMapper.selectByKeyword(keyword);
        PageInfo<Customer> page = new PageInfo<>(customers);
        return page;
    }

    @Override
    public Customer findById(int id) {
        return customerMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean modify(Customer customer) {
        return customerMapper.updateById(customer) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeById(int id) {
        return customerMapper.deleteById(id) > 0;
    }
}
